namespace Soyuz5.Utility
{
    public static class Configuration
    {
        public static void ProtectConnectionString(string executablePath)
        {
            ToggleConnectionStringProtection(executablePath, true);
        }

        public static void UnprotectConnectionString(string executablePath)
        {
            ToggleConnectionStringProtection(executablePath, false);
        }

        private static void ToggleConnectionStringProtection(string executablePath, bool protect)
        {
            // Define the Dpapi provider name.
            string provider = "DataProtectionConfigurationProvider";
            // string strProvider = "RSAProtectedConfigurationProvider";

            System.Configuration.Configuration config = null;
            System.Configuration.ConnectionStringsSection section = null;

            try
            {
                // Open the configuration file and retrieve 
                // the connectionStrings section.

                // For Web!
                // oConfiguration = System.Web.Configuration.
                //                  WebConfigurationManager.OpenWebConfiguration("~");

                // For Windows!
                // Takes the executable file name without the config extension.
                config = System.Configuration.ConfigurationManager.
                                                OpenExeConfiguration(executablePath);

                if (config != null)
                {
                    bool changed = false;

                    section = config.ConnectionStrings;

                    if (section != null)
                    {
                        if ((!(section.ElementInformation.IsLocked)) &&
                (!(section.SectionInformation.IsLocked)))
                        {
                            if (protect)
                            {
                                if (!(section.SectionInformation.IsProtected))
                                {
                                    changed = true;

                                    // Encrypt the section.
                                    section.SectionInformation.ProtectSection
                                (provider);
                                }
                            }
                            else
                            {
                                if (section.SectionInformation.IsProtected)
                                {
                                    changed = true;

                                    // Remove encryption.
                                    section.SectionInformation.UnprotectSection();
                                }
                            }
                        }

                        if (changed)
                        {
                            // Indicates whether the associated configuration section 
                            // will be saved even if it has not been modified.
                            section.SectionInformation.ForceSave = true;

                            // Save the current configuration.
                            config.Save();
                        }
                    }
                }
            }
//            catch (System.Exception ex)
//            {
//                throw (ex);
//            }
            finally
            {
            }
        }
    }
}